<template>
  <div class="mod-role">
    <el-form :form="query" :inline="true" @keyup.enter.native="searchDict()">
      <el-form-item>
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addDict">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      style="width: 100%">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="name"
        align="center"
        label="描述">
      </el-table-column>

      <el-table-column
        prop="type"
        align="center"
        label="类型标识">
      </el-table-column>

      <el-table-column
        prop="sort"
        align="center"
        label="排序">
      </el-table-column>

      <el-table-column
        prop="remark"
        align="center"
        label="备注">
      </el-table-column>

      <el-table-column
        prop="create_date"
        align="center"
        :formatter="formatterCreateDate"
        label="创建时间">
      </el-table-column>

       <el-table-column min-width="150" align="center" label="操作">
         <template slot-scope="scope">
           <el-button icon="el-icon-edit" @click="editDictValue(scope.row)" type="warning" size="mini">编辑</el-button>
           <el-button icon="el-icon-delete" @click="deleteDictValue(scope.row)" type="danger" size="mini">删除</el-button>
           <el-button icon="el-icon-menu" @click="showDictValue(scope.row)" type="primary" plain size="mini">字典值</el-button>
         </template>
       </el-table-column>
    </el-table>

    <dict-form
      :showFlag="showFlag"
      @closeDialog="closeDialog"
      @close="closeForm"
      :formData="dictForm">
    </dict-form>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  import dictForm from '@/components/system/dict-form'
  export default {
    components: {dictForm},
    data () {
      return {
        dataList: [],
        loading: true,
        time: '',
        dictForm: {
          name: '',
          type: '',
          sort: 0,
          remark: ''
        },
        showFlag: false,
        query: {
          startTime: '',
          endTime: ''
        },
        currentPage: 1,
        pageSize: 10,
        totalCount: 0
      }
    },

    mounted () {
      this.getDictList()
    },

    methods: {

      addDict () {
        this.showFlag = true
      },

      showDictValue (item) {
        this.$router.push({
          path: 'dictValue',
          query: {
            id: item.id
          }
        })
      },

      closeDialog () {
          this.showFlag = false
      },

      closeForm () {
          this.showFlag = false
          this.getDictList()
      },

      searchDict () {
        this.currentPage = 1
        this.getDictList()
      },

      handleSizeChange (val) {
        this.pageSize = val
        this.currentPage = 1
        this.getDictList()
      },

      formatterCreateDate (row, column) {
        if (!row.create_date) {
          return '--'
        }
        return this.moment(row.create_date).format('YYYY-MM-DD HH:mm')
      },

      handleCurrentChange (val) {
        this.currentPage = val
        this.getDictList()
      },

      editDictValue (item) {
        this.dictForm = {
            name: item.name,
            type: item.type,
            sort: item.sort,
            remark: item.remark,
            id: item.id
        }
        this.showFlag = true
      },

      handelPotentialRangeChange (val) {
        if (val && val instanceof Array && val.length === 2) {
          this.query.startTime = val[0]
          this.query.endTime = val[1]
        }
      },
      deleteById (item) {
        item.sqlId = 'system.common.deleteById'
        item.table = 'system_log'
        let that = this
        that.$confirm('确定删除选中的日志?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          that.axios.delete(that.$http.httpUrl('/system/log'), {
            data: item
          }).then(function (response) {
            if (response.data.code === 1) {
              that.$message({
                type: 'success',
                message: response.data.message
              })
              that.getLogs()
            } else {
              that.$message({
                type: 'error',
                message: response.data.message
              })
            }
          })
        })
      },

      getDictList () {
        let that = this
        that.axios.get(that.$http.httpUrl('/dict/list'), {
          params: {
            offset: (that.currentPage - 1) * that.pageSize,
            limit: that.pageSize,
          }
        }).then(function (response) {
          that.loading = false
          that.dataList = response.data.data.dataList
          that.totalCount = response.data.data.total
        })
      },

      /**
       * 删除字典类型
       * @param item
       */
      deleteDictValue (item) {
        this.$confirm('确定字典' + item.name + '?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.delete(this.$http.httpUrl('/dict/deleteDictById'), {
            data: item
          }).then(response => {
            if (response.data.code === 1) {
              this.$message.success(response.data.message)
              this.getDictList()
            } else {
              this.$message.error(response.data.message)
            }
          })
        })
      }
    }
  }
</script>
